#!/bin/ksh

file="$1"
src="$2"
hash="$3"

[ "$src" = merge ] || exit 0

gpath="`git rev-parse --git-path ''`"
format='commit %h%n%B'

tf=`mktemp`
(
if [ -f "$gpath/MERGE_HEAD" ]; then
	git log --format="$format" --reverse ORIG_HEAD..MERGE_HEAD | sed -E '
		/^[0-9]{4,5} /s/^/    /
		/^$/d
	'
	echo
elif [ -f "$gpath/CHERRY_PICK_HEAD" ]; then
	git log --format="commit %h" --reverse \
	    CHERRY_PICK_HEAD~..CHERRY_PICK_HEAD
	echo
fi
) > $tf

firstblank="`egrep -n '^$' "$file" | sed 's/://; q'`"
[ -n "$firstblank" ] && sed -i "${firstblank}r$tf" "$file"

rm -f $tf

sed -i -E '
	/^# Conflicts:/,/^#$/s/^# *//
	/^(Reviewed|(Portions )?[Cc]ontributed|Approved|Contributed)[- ][Bb]y/d
' "$file"

exit 0

